<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <!-- 左侧筛选项 -->
      <div class="container-layout-left">
        <el-cascader :options="responseData.data" :props="{ checkStrictly: true }" v-model="form.superLabel" size="mini"></el-cascader>
      </div>
      <!-- 右侧操作栏 -->
      <div class="container-layout-right">
        <el-button type="primary" size="mini" icon="el-icon-search" @click="handleOpenSavePanel">新增</el-button>
        <el-button type="danger" size="mini" icon="el-icon-search" @click="handleDeleteButtonClick">删除</el-button>
      </div>
      <div style="float: none"></div>
    </div>
    <!-- 页面数据列表 -->
    <div class="data-table">
      <el-tabs :tab-position="pageData.modelData.tabPosition" style="height: auto;" @contextmenu.prevent.native="handleOpenMouse($event)">
        <!--遍历第一层标签-->
        <el-tab-pane  v-for="first in responseData.data" :key="first.value" :label="first.label" :name="first.value.toString()">
          <el-tabs v-if="first.children !== []" :tab-position="pageData.modelData.tabPosition" style="height: auto;" @contextmenu.prevent.native="handleOpenMouse($event)">
            <!--遍历第二层标签-->
            <el-tab-pane v-for="second in first.children" :key="second.value" :label="second.label" :name="second.value.toString()">
              <el-tabs v-if="second.children !== []" :tab-position="pageData.modelData.tabPosition" style="height: auto;" @contextmenu.prevent.native="handleOpenMouse($event)">
                <!--遍历第三层标签-->
                <el-tab-pane v-for="third in second.children" :key="third.value" :label="third.label" :name="third.value.toString()">
                  <el-tabs v-if="third.children !== []" :tab-position="pageData.modelData.tabPosition" style="height: auto;" @contextmenu.prevent.native="handleOpenMouse($event)">
                    <!--遍历第四层标签-->
                    <el-tab-pane v-for="fourth in third.children" :key="fourth.value" :label="fourth.label" :name="fourth.value.toString()">
                      <el-tabs v-if="fourth.children !== []" :tab-position="pageData.modelData.tabPosition" style="height: auto;" @contextmenu.prevent.native="handleOpenMouse($event)">
                        <!--遍历第五层标签-->
                        <el-tab-pane v-for="fifth in fourth.children" :key="fifth.value" :label="fifth.label" :name="fifth.value.toString()"></el-tab-pane>
                      </el-tabs>
                    </el-tab-pane>
                  </el-tabs>
                </el-tab-pane>
              </el-tabs>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 新增/修改隐藏域 -->
    <el-dialog :title="pageData.savePanelTitle" :visible.sync="pageData.savePanelStatus" size="tiny" width="480px"
               :before-close="handleCloseSavePanel">
      <el-card :body-style="{ padding: '0px' }">
        <el-input v-model="form.label"/>
      </el-card>
      <el-button type="primary" size="small" style="margin-top: 30px" @click="handleSaveButtonClick">保存数据</el-button>
    </el-dialog>
    <!-- 鼠标右键 -->
    <ul v-show="pageData.modelData.contextMenuVisible" :style="{left:pageData.modelData.left+'px',top:pageData.modelData.top+'px'}" class="contextmenu">
      <li @click="handleOpenEditPanel">修改</li>
    </ul>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsExamLabelList,saveCmsExamLabelData,updateCmsExamLabelData,deleteCmsExamLabelData } from "@/api/study";
  export default {
    name: "studyLabel",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          modelData:{
            loading: false,
            tabPosition: 'left',
            inputVisible: false,
            savePanel: false,
            contextMenuVisible: false,
            left: 0,
            top: 0,
            editPanel: false
          },
          savePanelStatus: false,
          savePanelType: 0,
          savePanelTitle: "",
        },// 页面配置数据
        form:{
          superLabel: [],
          superId: "",
          label: "",
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          data: [],
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {
      'pageData.modelData.savePanel': function (newValue, oldValue) {
        if (!newValue) {
          this.form = {label: '', superData: [], superId: '', id: ''}
        }
      },
      'pageData.modelData.editPanel': function (newValue, oldValue) {
        if (!newValue) {
          this.form = {label: '', superData: [], superId: '', id: ''}
        }
      },
      'pageData.modelData.contextMenuVisible'(value) {
        if (this.pageData.modelData.contextMenuVisible) {
          document.body.addEventListener("click", this.closeContextMenu);
        } else {
          document.body.removeEventListener("click", this.closeContextMenu);
        }
      }
    },
    //方法集合
    methods:{
      // 方法函数
      handlePageInit(){
        this.pageData.modelData.loading = true;
        this.loadCmsExamLabelList();
      },// 初始化数据
      handleOpenSavePanel(){
        if (PermissionsToDetectFormUser(getPermission(), "710001")){
          if (this.form.superLabel === [] || this.form.superLabel.length === 0){
            this.$message.warning("添加考点标签之前，需先选中父级项");
          } else {
            // 初始化面板
            this.form.superId = this.form.superLabel[this.form.superLabel.length - 1];
            // 打开面板
            this.pageData.savePanelStatus = true;
            this.pageData.savePanelType = 1;
            this.pageData.savePanelTitle = "考点新增";
          }
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开新增面板
      handleCloseSavePanel(){
        this.$confirm("确认关闭？").then(response => {
          // 初始化表单
          this.form.superLabel = [];
          this.form.superId = "";
          // 打开面板
          this.pageData.savePanelStatus = false;
          this.pageData.savePanelType = 0;
          this.pageData.savePanelTitle = "";
        }).catch(err => {
          console.log(err);
        });
      },// 关闭新增面板
      handleOpenEditPanel(){
        if (PermissionsToDetectFormUser(getPermission(), "710003")){
          this.pageData.modelData.contextMenuVisible = false;
          this.pageData.savePanelStatus = true;
          this.pageData.savePanelType = 2;
          this.pageData.savePanelTitle = "考点修改";
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开修改面板
      handleSaveButtonClick(){
        if (this.pageData.savePanelType === 1){
          if (!PermissionsToDetectFormUser(getPermission(), "710001")){
            this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
            return;
          }
        }
        if (this.pageData.savePanelType === 2){
          if (!PermissionsToDetectFormUser(getPermission(), "710003")){
            this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
            return;
          }
        }
        this.$confirm('是否保存当前考点, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.pageData.savePanelType === 1){
            this.saveCmsExamLabelData();
          } else if (this.pageData.savePanelType === 2){
            this.updateCmsExamLabelData();
          }
        }).catch(cancel => {
          console.log(cancel);
          this.$message.info("已取消操作");
        });
      },// 保存考点数据
      handleOpenMouse(event){
        if (event.srcElement.id !== '') {
          this.pageData.modelData.left = event.pageX;
          this.pageData.modelData.top = event.pageY + 10;
          this.pageData.modelData.contextMenuVisible = true;
          this.form.id = event.srcElement.id.split("-")[1];// 拿到ID参数
          this.form.label = event.srcElement.innerHTML;// 拿到原本的label参数
        }
      },// 鼠标右键开启修改按钮(后续可增加删除)
      handleDeleteButtonClick(){
        if (PermissionsToDetectFormUser(getPermission(), "710002")){
          this.$confirm("该操作可能会影响考试，请谨慎选择？").then(response => {
            // 初始化表单
            let param = {id: this.form.superLabel[this.form.superLabel.length-1]}
            this.deleteCmsExamLabelData(param);
          }).catch(err => {
            console.log(err);
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 删除按钮点击事件
      // 接口
      loadCmsExamLabelList(){
        loadCmsExamLabelList().then(response => {
          if (response.code === "200"){
            this.responseData.data = response.data;
            this.pageData.modelData.loading = false;
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        })
      },// 获取考点标签数据
      saveCmsExamLabelData(){
        saveCmsExamLabelData(this.form).then(response =>{
          if (response.code === "200"){
            this.$message.success("考点保存完成");
            // 初始化表单
            this.form.superLabel = [];
            this.form.superId = "";
            // 打开面板
            this.pageData.savePanelStatus = false;
            this.pageData.savePanelType = 0;
            this.pageData.savePanelTitle = "";
            this.handlePageInit();
          } else {
            console.log(response.message);
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        });
      },// 保存考点标签数据
      updateCmsExamLabelData(){
        updateCmsExamLabelData(this.form).then(response =>{
          if (response.code === "200"){
            this.$message.success("考点保存完成");
            // 初始化表单
            this.form.superLabel = [];
            this.form.superId = "";
            // 打开面板
            this.pageData.savePanelStatus = false;
            this.pageData.savePanelType = 0;
            this.pageData.savePanelTitle = "";
            this.handlePageInit();
          } else {
            console.log(response.message);
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        });
      },// 更新考点标签
      deleteCmsExamLabelData(param){
        deleteCmsExamLabelData(param).then(response =>{
          if (response.code === "200"){
            this.$message.success("考点删除完成");
            this.handlePageInit();
          } else {
            console.log(response.message);
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        });
      },// 更新考点标签
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      // 进行初始化数据读取
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .filter-container {
    float: none;
    width: 100%;
    height: 50px;
  }
  .container-layout-left {
    float: left;
    text-align: left;
    width: 500px;
  }
  .container-layout-right {
    float: left;
    text-align: left;
  }
  .filter-item{
    width: 420px;
  }
  .data-table{
    width: 100%;
  }
  .contextmenu {
    width: 100px;
    margin: 0;
    border: 1px solid #ccc;
    background: #fff;
    z-index: 3000;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 14px;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.2);
  }
  .contextmenu li {
    margin: 0;
    padding: 7px 16px;
  }
  .contextmenu li:hover {
    background: #f2f2f2;
    cursor: pointer;
  }
</style>
